package com.zzt.video.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.List;

@Configuration
public class CorsGlobalConfig {

    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();

        // ✅ 放通你所有部署可能用到的来源
        config.setAllowedOrigins(List.of(
                "http://localhost:3000",
                "https://abbas-lose-disk.com",
                "https://www.abbas-lose-disk.com",
                "https://back-service.abbas-lose-disk.com"
        ));

        config.setAllowCredentials(true); // 必须配合具体 origin，不能配 *！

        // 方法、头设置
        config.setAllowedMethods(List.of("GET", "POST", "OPTIONS"));
        config.setAllowedHeaders(List.of("*"));
        config.setExposedHeaders(List.of("Content-Disposition")); // 如果你下载文件
        config.setMaxAge(3600L); // 预检缓存

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);

        return new CorsFilter(source);
    }
}
